package com.dayi.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Camera;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.dayi.R;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class VerticalScrollView extends ViewFlipper {

    private Context mContext;
    //是否开启动画
    private boolean isSetAnimDuration = false;
    //时间间隔
    private int interval = 3000;
    //动画时间
    private int animDuration = 500;

    public VerticalScrollView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public VerticalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
        setFlipInterval(interval);
        Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
        if (isSetAnimDuration) animIn.setDuration(animDuration);
        setInAnimation(animIn);
        Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
        if (isSetAnimDuration) animOut.setDuration(animDuration);
        setOutAnimation(animOut);
    }

    /**
     * 设置循环滚动的View数组
     *
     * @param
     */
    public void setViews(int type, final List<VerticalData> datas) {
        if (datas == null || datas.size() == 0) return;
        removeAllViews();
        int size = datas.size();
        for (int i = 0; i < size; i++) {
            final int position = i;
            //根布局
            LinearLayout item = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.vertical_item_view, null);
            //设置监听
            item.findViewById(R.id.lin_view).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position);
                    }
                }
            });

            //控件赋值
            ImageView imageView1 = item.findViewById(R.id.iv_head);
            TextView textView1 = item.findViewById(R.id.tv_content);
            TextView textViewTime1 = item.findViewById(R.id.tv_time);

            textView1.setText(datas.get(position).getTvContent());
            textViewTime1.setText(datas.get(position).getTvTime());

            if ("1".equals(datas.get(position).getImgType())) {
                imageView1.setImageResource(R.mipmap.icon_teacher);
            } else {
                imageView1.setImageResource(R.mipmap.icon_student);
            }

            if (0 == type) {
                imageView1.setVisibility(INVISIBLE);
                textView1.setTextColor(Color.WHITE);
                textView1.setTextSize(20);
                textViewTime1.setTextColor(Color.WHITE);
                textViewTime1.setTextSize(20);

            }

            addView(item);
        }
        startFlipping();
    }

    public void hideUserIcon() {


    }


    private OnItemClickListener onItemClickListener;

    /**
     * 设置监听接口
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * item_view的接口
     */
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

}


